﻿using MyProject.Core;
using MyProject.Entities.Config;
using System.Data;
using Item = MyProject.Entities.ListItem.ListItem;

namespace MyProject.Services.ListItem
{
    public class ListItemService
    {
        public readonly Dictionary<int, List<Item>> Lists = new();

        private Item? Department;
        private Item? Address;

        private static ListItemService instance;

        public ListItemService()
        {
            this.BuildLists();
        }

        private async void BuildLists()
        {
            var dictionary = this.Lists;

            using (MyDbContext db = new MyDbContext())
            {
                var queryRows = db.ListItem.Where(row => row.alias == "departments");
                if (queryRows.Count() > 0)
                {
                    this.Department = queryRows.FirstOrDefault();
                    IQueryable<Item> departments = db.ListItem.Where(row => row.p_id == this.Department.id);
                    dictionary.Add((int)this.Department.id, departments.ToList());
                }

                queryRows = db.ListItem.Where(row => row.alias == "address");
                if (queryRows.Count() > 0)
                {
                    this.Address = queryRows.FirstOrDefault();
                    if (this.Address != null)
                    {
                        queryRows = db.ListItem.Where(row => row.p_id == (int)this.Address.id);
                        dictionary.Add((int)this.Address.id, queryRows.ToList());
                        foreach (Item city in queryRows)
                        {
                            IQueryable<Item> streets = db.ListItem.Where(row => row.p_id == city.id && row.alias == "street");
                            dictionary.Add((int)city.id, streets.ToList());
                        }
                    }
                }
            }
        }

        public Dictionary<int, string> getFlatList()
        {
            Dictionary<int, string> list = new Dictionary<int, string>();

            using (MyDbContext db = new MyDbContext())
            {
                
                var rows = db.ListItem.ToList();
                foreach (var row in rows)
                {
                    if (row.p_id != 0)
                    {
                        list.Add((int)row.id, row.name);
                    }
                }
            }

            return list;
        }

        public static ListItemService getInstance()
        {
            if (instance == null)
            {
                instance = new ListItemService();
            }

            return instance;
        }

        public List<Item> Departments
        {
            get
            {
                if (this.Department != null)
                {
                    return this.Lists[(int)this.Department.id];
                }
                return new List<Item>();
            }
        }
        public List<Item> Cities
        {
            get
            {
                if (this.Address != null)
                {
                    return this.Lists[(int)this.Address.id];
                }
                return new List<Item>();
            }
        }
    }
}
